﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace br.ita.CTC34.GLCToAPConverter
{
    public partial class InputForm : Form
    {
        private GLC glc { get; set; }
        private Automata ap { get; set; }

        private static string FILE_SAVED = "Seu arquivo foi salvo com sucesso!";

        public InputForm()
        {
            InitializeComponent();
        }

        private void btnInputDialog_Click(object sender, EventArgs e)
        {
            InputFileDialog.ShowDialog();
            if (InputFileDialog.FileName != null && !InputFileDialog.FileName.Equals(""))
            {
                this.glc = IOManager.parseInputFile(InputFileDialog.FileName);
                txtAreaGLC.Text = "";
                txtAreaGLC.Text = this.glc.ToString();
                setGLCTextAreaSectionsTitlesToBold();

                txtAreaAP.Text = "";
                btnConvert.Enabled = true;
                btnSaveAutomata.Enabled = false;
            }
        }

        private void setGLCTextAreaSectionsTitlesToBold()
        {
            string text = txtAreaGLC.Text;

            int initialSymbolIndex = text.IndexOf(GLC.INITIAL_SYMBOL);
            int emptyStringSymbolIndex = text.IndexOf(GLC.EMPTY_STRING_SYMBOL);
            int terminalSymbolsIndex = text.IndexOf(GLC.TERMINAL_SYMBOLS);
            int nonTerminalSymbolsIndex = text.IndexOf(GLC.NON_TERMINAL_SYMBOLS);
            int productionsIndex = text.IndexOf(GLC.PRODUCTIONS);

            // Símbolo Inicial
            txtAreaGLC.Select(initialSymbolIndex,GLC.INITIAL_SYMBOL.Length+1);
            txtAreaGLC.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolo da Cadeia Vazia
            txtAreaGLC.Select(emptyStringSymbolIndex, GLC.EMPTY_STRING_SYMBOL.Length+1);
            txtAreaGLC.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolos Terminais
            txtAreaGLC.Select(terminalSymbolsIndex, GLC.TERMINAL_SYMBOLS.Length +1 );
            txtAreaGLC.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolos Não-Terminais
            txtAreaGLC.Select(nonTerminalSymbolsIndex, GLC.NON_TERMINAL_SYMBOLS.Length + 1);
            txtAreaGLC.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Produções
            txtAreaGLC.Select(productionsIndex, GLC.PRODUCTIONS.Length + 1);
            txtAreaGLC.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            this.ap = Converter.GLCtoAP(this.glc);

            if (this.ap != null)
            {
                txtAreaAP.Text = "";
                txtAreaAP.Text = this.ap.ToString();
                setAPTextAreaSectionsTitlesToBold();
                
                btnSaveAutomata.Enabled = true;
                btnConvert.Enabled = false;
            }
        }

        private void setAPTextAreaSectionsTitlesToBold()
        {
            string text = txtAreaAP.Text;

            int initialStateIndex = text.IndexOf(Automata.INITIAL_STATE);
            int emptyStringSymbolIndex = text.IndexOf(Automata.EMPTY_STRING_SYMBOL);
            int emptyStackSymbolIndex = text.IndexOf(Automata.EMPTY_STACK_SYMBOL);
            int finalStatesIndex = text.IndexOf(Automata.FINAL_STATES);
            int tapeSymbolsIndex = text.IndexOf(Automata.TAPE_SYMBOLS);
            int stackSymbolsIndex = text.IndexOf(Automata.STACK_SYMBOLS);
            int transitionsIndex = text.IndexOf(Automata.TRANSITIONS);

            // Estado Inicial
            txtAreaAP.Select(initialStateIndex, Automata.INITIAL_STATE.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolo da Cadeia Vazia
            txtAreaAP.Select(emptyStringSymbolIndex, Automata.EMPTY_STRING_SYMBOL.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolo da Pilha Vazia
            txtAreaAP.Select(emptyStackSymbolIndex, Automata.EMPTY_STACK_SYMBOL.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Estados Finais
            txtAreaAP.Select(finalStatesIndex, Automata.FINAL_STATES.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolos da Fita
            txtAreaAP.Select(tapeSymbolsIndex, Automata.TAPE_SYMBOLS.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Símbolos da Pilha
            txtAreaAP.Select(stackSymbolsIndex, Automata.STACK_SYMBOLS.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);

            //Transições
            txtAreaAP.Select(transitionsIndex, Automata.TRANSITIONS.Length + 1);
            txtAreaAP.SelectionFont = new Font(txtAreaGLC.SelectionFont, FontStyle.Bold);
        }

        private void btnSaveAutomata_Click(object sender, EventArgs e)
        {
            OutputSaveFileDialog.ShowDialog();

            string filename = OutputSaveFileDialog.FileName;
            OutputSaveFileDialog.FileName = "";

            if (filename != null && !filename.Equals(""))
            {
                int dotExtensionIndex = filename.LastIndexOf('.');

                if(dotExtensionIndex == -1 || dotExtensionIndex != (filename.Length - 4))
                {
                    filename += ".txt";
                }

                IOManager.writeOutputFile(this.ap, filename);

                MessageBox.Show(FILE_SAVED);

                clearAPTextArea();
                clearGLCTextArea();

                this.glc = null;
                this.ap = null;

                btnConvert.Enabled = false;
                btnSaveAutomata.Enabled = false;
            }
        }

        private void clearGLCTextArea()
        {
            this.txtAreaGLC.Text = "";
        }

        private void clearAPTextArea()
        {
            this.txtAreaAP.Text = "";
        }
    }
}